package com.wudi.estate.service.impl.communitymanagement;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wudi.estate.bean.communitymanagement.Community;
import com.wudi.estate.bean.communitymanagement.Develope;
import com.wudi.estate.bean.communitymanagement.PmCompany;
import com.wudi.estate.bean.homemanage.Buildings;
import com.wudi.estate.bean.homemanage.Homes;
import com.wudi.estate.mapper.communitymanagement.CommunityMapper;
import com.wudi.estate.mapper.communitymanagement.DevelopeMapper;
import com.wudi.estate.mapper.communitymanagement.PmCompanyMapper;
import com.wudi.estate.mapper.homemanage.BuildingsMapper;
import com.wudi.estate.mapper.homemanage.HomesMapper;
import com.wudi.estate.service.communitymanagement.CommunityService;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CommunityServiceImpl implements CommunityService {
    @Autowired
    private CommunityMapper communityMapper;
    @Autowired
    private BuildingsMapper buildingsMapper;
    @Autowired
    private HomesMapper homesMapper;
    @Autowired
    private DevelopeMapper developeMapper;
    @Autowired
    private PmCompanyMapper pmCompanyMapper;

    @Override
    public Community selectById(Community c) throws Exception {
        return communityMapper.selectById(c.getId());
    }

    @Override
    public void insert(Community c) throws Exception {
        communityMapper.insert(c);
    }

    @Override
    public void updateById(Community c) throws Exception {
        communityMapper.updateById(c);
    }

    @Override
    public List<Community> select() throws Exception {
        return communityMapper.selectList(null);
    }

    @Override
    public Community selectById(long id) throws Exception {
        return communityMapper.selectById(id);
    }

    @Override
    public List<Map<String, Object>> selectCommunityAndBuildsAndHouses(){
        List<Map<String,Object>> lists = new ArrayList<>();
        QueryWrapper<Buildings> queryWrapperBuildings = new QueryWrapper();
        QueryWrapper<Homes> queryWrapperHomes= new QueryWrapper();
        List<Community> communities = communityMapper.selectList(null);
        for (Community community : communities) {
            Develope develope = developeMapper.selectById(community.getDid());
            PmCompany pmCompany = pmCompanyMapper.selectById(community.getPid());
            Map<String,Object> maps = new HashMap<>();
            queryWrapperBuildings.eq("cid",community.getId());
            Long  buildNum  = buildingsMapper.selectCount(queryWrapperBuildings);
            List<Buildings> buildings = buildingsMapper.selectList(queryWrapperBuildings);
            long  homes =0;
            for (Buildings building : buildings) {
                queryWrapperHomes.eq("bid",building.getId());
                Long homeNum = homesMapper.selectCount(queryWrapperHomes);
                homes+=homeNum;
                queryWrapperHomes.clear();
            }
            queryWrapperBuildings.clear();
            maps.put("pmCompany",pmCompany);
            maps.put("develope",develope);
            maps.put("communityById",community);
            maps.put("buildNum",buildNum);
            maps.put("homes",homes);
            lists.add(maps);
        }
        return lists;
    }
    //删除为更改
    @Override
    public void deleteById(long id) {
        Community community = communityMapper.selectById(id);
        community.setState(!community.isState());
        communityMapper.updateById(community);
    }
}


